package com.company.ecis.service.impl;

import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.ecis.entity.Userinfo;
import com.company.ecis.entity.vo.UserinfoSmallVO;
import com.company.ecis.entity.vo.UserinfoVO;
import com.company.ecis.mapper.UserinfoMapper;
import com.company.ecis.service.UserinfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2022-08-04
 */
@Service
public class UserinfoServiceImpl extends ServiceImpl<UserinfoMapper, Userinfo> implements UserinfoService {

    @Autowired
    private UserinfoMapper userinfoMapper;

    @Override
    public Userinfo selectOneByUsernameAndPassword(UserinfoVO userinfoVO) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("username",userinfoVO.getUsername());
        wrapper.eq("password", SecureUtil.md5(userinfoVO.getPassword()));
        return userinfoMapper.selectOne(wrapper);
    }

    @Override
    public boolean insertUserinfo(Userinfo userinfo) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<Userinfo>().eq("pid",userinfo.getPid())
                .or().eq("phone",userinfo.getPhone())
                .or().eq("username",userinfo.getUsername());
        Userinfo temp = userinfoMapper.selectOne(wrapper);
        if (temp != null){
            return false;
        }
        userinfo.setPassword(SecureUtil.md5(userinfo.getPassword()));
        userinfoMapper.insert(userinfo);
        return true;
    }

    @Override
    public Userinfo selectOneByUserIdAndPayPassword(int userId,int payPassword) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id",userId).eq("password",SecureUtil.md5(String.valueOf(payPassword)));
        return userinfoMapper.selectOne(wrapper);
    }

    @Override
    public Userinfo selectOneByUserIdAndUsername(int userId, String username) {
        QueryWrapper<Userinfo> wrapper = new QueryWrapper<>();
        wrapper.eq("id",userId).eq("username",username);
        return userinfoMapper.selectOne(wrapper);
    }
}
